/**
 * Created by Zhao on 2017/3/13.
 */

/*
 * 将itemCat数据生成 tree
 */
function transTree(tree,data) {
    appendTree(0, tree, data);
}

/**
 * 获得树
 * @param tree
 */
function getItemCatTree(tree){
    $.ajax({
        url:'../resources/itemCatData.json',
        success:function (data) {
            transTree(tree, data);
        },
    });
}

function appendTree(parentId,tree,data) {
    var node;
    for (var i=0;i<data.length&&data[i];i++){
        node = data[i];

        if (parentId==node.parentId){
            data.splice(i, 1);
            i--;
            //子节点
            var cNode = {
                id:node.id,
                label:node.name,
                children:[]
            };
            tree.push(cNode);
            if (node.isParent){
                //递归搜索
                appendTree(node.id,cNode.children,data);
            }
        }
    }
}